package service.impl;

import dao.CommentDao;
import dao.impl.CommentDaoImpl;
import entity.Comment;
import entity.Order;
import entity.PageBean;
import service.CommentService;

import java.util.List;

public class CommentServiceImpl implements CommentService {
    private CommentDao dao = new CommentDaoImpl();

    @Override
    public List<Comment> findCommentByProductId(int productId) {
        return dao.findCommentByProductId(productId);
    }

    @Override
    public void addComment(Comment comment) {
        dao.addComment(comment);
    }

    @Override
    public PageBean<Comment> findCommentByPage(String _currentPage, String _rows) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //创建新的PageBean对象
        PageBean<Comment> pb = new PageBean<Comment>();

        //设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //调用dao查询总记录数
        int totalCount = dao.findTotalCount();
        pb.setTotalCount(totalCount);

        //调用dao查询List集合
        //计算开始记录的索引
        int start = (currentPage - 1) * rows;
        List<Comment> list = dao.findByPage(start,rows);
        pb.setList(list);

        //计算总页码
        int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void delCommentById(int commentId) {
        dao.delCommentById(commentId);
    }
}
